<?php

namespace app\admin\controller;

use app\common\sms\SmsService;
use app\model\Staff;
use app\model\Enter;
use app\model\System;
use app\model\OnJob;
use app\model\User;

class Index extends Auth
{
    /**
     * 首页数据
     *
     * @return void
     */
    public function index()
    {
        $d_start = strtotime(date('Y-m-d', time())); // 今天开始时间
        $d_end = time();
        $last_d_start = $d_start - (7 * 86400);
        $last_d_end = $last_d_start + ($d_end - $d_start);

        $data = [
            'all_staff' => '',
            'staff_inc' => '',
            'wait_job_staff' => '',
            'on_job_staff' => '',
            'leave_job_staff' => '',
            'all_enter' => '',
            'enter_inc' => '',
            'enter_shi' => '',
            'enter_xian' => '',
            'enter_xiang' => '',
            'sms_count' => '',
            'sms_sum' => '',
            'register_count' => [],
            'all_register_count' => ''
        ];

        $d_staff = Staff::whereBetween('create_time', "{$d_start}, {$d_end}")->count();
        $last_d_staff = Staff::whereBetween('create_time', "{$last_d_start}, {$last_d_end}")->count() ?? 1;
        $data['all_staff'] = Staff::count();
        if (($d_staff - $last_d_staff) == 0 || $last_d_staff == 0) {
            $data['staff_inc'] = 0;
        } else {
            $data['staff_inc'] = ($d_staff - $last_d_staff) / $last_d_staff;
        }
        $data['wait_job_staff'] = Staff::waitOnJobList()['count'];
        $data['on_job_staff'] = OnJob::where('type', 1)->count();
        $data['leave_job_staff'] = OnJob::where('type', 3)->count();

        $d_enter = Enter::whereBetween('shenhe_time', "{$d_start}, {$d_end}")->count();
        $last_d_enter = Enter::whereBetween('shenhe_time', "{$last_d_start}, {$last_d_end}")->count();
        $data['all_enter'] = Enter::where('type', 0)->count();
        if (($d_enter - $last_d_enter) == 0 || $last_d_enter == 0) {
            $data['enter_inc'] = 0;
        } else {
            $data['enter_inc'] = ($d_enter - $last_d_enter) / $last_d_enter;
        }
        $data['enter_shi'] = Enter::where('shenfen', 1)->where('type', 0)->count();
        $data['enter_xian'] = Enter::where('shenfen', 2)->where('type', 0)->count();
        $data['enter_xiang'] = Enter::where('shenfen', 3)->where('type', 0)->count();

        $data['sms_count'] = System::where('id', 1)->value('sms_num');
        $data['sms_sum'] = $this->sumSms();
        $date = $this->get_days();
        foreach ($date as $k => $v) {
            $end_time = date('Y-m-d H:i:s', strtotime($v) + 66399);
            $count = User::where('create_time', 'between', "{$v}, {$end_time}")->count();
            $data['register_count'][] = ['date' => $v, 'count' => $count]; 
        }
        $data['all_register_count'] = User::count();
        success('首页数据', $data);
    }

    public function get_days($time = '', $format = 'Y-m-d')
    {
        $time = $time != '' ? $time : time();
        //组合数据
        $date = [];
        for ($i = 0; $i <= 29; $i++) {
            $date[$i] = date($format, strtotime('+' . $i - 29 . ' days', $time));
        }
        return $date;
    }

    /**
     * 查询剩余短信数量
     *
     * @return void
     */
    public function sumSms()
    {
        $num = SmsService::sumSms();
        return $num;
    }
}
